package com.example.dmgame.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.example.dmgame.R;
import com.example.dmgame.bean.ChapterListItem;
import com.example.dmgame.utils.AsyncBitmapLoader;
import com.example.dmgame.utils.AsyncBitmapLoader.ImageCallBack;
import com.example.dmgame.utils.AsyncImageLoader;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class NewsAdapter extends BaseAdapter {
	List<ChapterListItem> list;
	Context context;
	ListView listView;
	
//	AsyncImageLoader asyncImageLoader;
	AsyncBitmapLoader asyncBitmapLoader;

	private String baseUrl = "http://www.3dmgame.com";

	public NewsAdapter(List<ChapterListItem> list, Context context,
			ListView listView) {
		super();
		this.list = list;
		this.context = context;
		this.listView = listView;
//		asyncImageLoader = new AsyncImageLoader();
		asyncBitmapLoader = new AsyncBitmapLoader();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.chapterlistitem, null);
			viewHolder = new ViewHolder();
			viewHolder.imageView = (ImageView) convertView
					.findViewById(R.id.cliLitpic);
			viewHolder.titleTv = (TextView) convertView
					.findViewById(R.id.cliTitle);
			viewHolder.senddateTv = (TextView) convertView
					.findViewById(R.id.cliSenddate);
			viewHolder.feedbackTv = (TextView) convertView
					.findViewById(R.id.cliFeedback);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		ChapterListItem chapterListItem = list.get(position);
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
		Date date = new Date(Long.parseLong(chapterListItem.getSenddate()));
		viewHolder.titleTv.setText(chapterListItem.getTitle());
		viewHolder.senddateTv.setText(format.format(date));
		viewHolder.feedbackTv.setText(chapterListItem.getFeedback() + "评论");

		String imageUrl = baseUrl + chapterListItem.getLitpic();
		viewHolder.imageView.setTag(imageUrl);
		
		Bitmap bitmap = asyncBitmapLoader.loadBitmap(viewHolder.imageView, imageUrl, new ImageCallBack() {
			
			public void imageLoad(ImageView imageView, Bitmap bitmap) {
				// TODO Auto-generated method stub
				imageView.setImageBitmap(bitmap);
			}
		});
		if(bitmap == null){
			viewHolder.imageView.setImageResource(R.drawable.product_default);
		}else{
			viewHolder.imageView.setImageBitmap(bitmap);
		}
		return convertView;
	}

	private final class ViewHolder {
		public ImageView imageView;
		public TextView titleTv;
		public TextView senddateTv;
		public TextView feedbackTv;
	}

}
